﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace ConvertTextToPDF
{
    public class Biz
    {
        private static readonly Biz instance = new Biz();

        private Events.OnWriteMsgOut onMsg = null;

        private Events.OnFindFileInfo onFoundReport = null;

        private Events.OnFindDirInfo onFoundDirReport = null;

        private Events.OnReportProcess onReportProcess = null;

        public static Biz Instance
        {
            get
            {
                return instance;
            }
        }

        private Biz() { }

        public Events.OnWriteMsgOut OnMsg
        {
            get
            {
                return onMsg;
            }

            set
            {
                onMsg = value;
            }
        }

        public Events.OnFindFileInfo OnFound
        {
            get
            {
                return OnFoundReport;
            }

            set
            {
                OnFoundReport = value;
            }
        }

        public Events.OnFindDirInfo OnFoundDirReport
        {
            get
            {
                return onFoundDirReport;
            }

            set
            {
                onFoundDirReport = value;
            }
        }

        public Events.OnFindFileInfo OnFoundReport
        {
            get
            {
                return onFoundReport;
            }

            set
            {
                onFoundReport = value;
            }
        }

        public Events.OnReportProcess OnReportProcess
        {
            get
            {
                return onReportProcess;
            }

            set
            {
                onReportProcess = value;
            }
        }

        public void ConvertTextToPDF(string filePath, Events.OnConvertComplate onComplate)
        {
            MemoryStream ms = null;

            ms = new MemoryStream();

            try
            {
                using (TextReader tr = new StreamReader("test.txt"))
                {
                    //Instantiate Pdf pbject by calling its empty constructor
                    Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
                    //Create a new section in the Pdf object
                    Aspose.Pdf.Generator.Section sec = pdf.Sections.Add();
                    //Create a new text paragraph and pass the text to its constructor as argument
                    Aspose.Pdf.Generator.Text txt = new Aspose.Pdf.Generator.Text(tr.ReadToEnd());

                    sec.Paragraphs.Add(txt);

                    pdf.Save(ms);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            onComplate(ms.ToArray());
        }

        public void FindAllTextFileList(string dirPath, string filter, Events.OnFindFileList onFound)
        {
            List<FileInfo> result = null;

            onReportProcess(0);

            result = GetFileList(dirPath, filter);

            onFound(result);
        }

        private List<FileInfo> GetFileList(string dirPath, string filter)
        {
            List<FileInfo> result = null;

            result = new List<FileInfo>();

            DirectoryInfo di = new DirectoryInfo(dirPath);

            DirectoryInfo[] dirs = di.GetDirectories();

            List<FileInfo> files = new List<FileInfo>(di.GetFiles());

            int i = 0;

            foreach (DirectoryInfo dir in dirs)
            {
                onFoundDirReport(dir);

                result.AddRange(GetFileList(dir.FullName, filter));
            }

            result.AddRange(files.FindAll(t => onReportProcess(Convert.ToInt32(i++ / files.Count * 1.0)) && OnFoundReport(t) && Regex.IsMatch(t.Extension, filter)));

            return result;
        }
    }

    public class Events
    {
        public delegate void OnConvertComplate(byte[] result);

        public delegate void OnFindFileList(List<FileInfo> list);

        public delegate bool OnFindFileInfo(FileInfo fileInfo);

        public delegate bool OnFindDirInfo(DirectoryInfo dirInfo);

        public delegate void OnWriteMsgOut(string msg);

        public delegate bool OnReportProcess(int i);
    }
}
